package com.cvs.DAO;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.Query;
import com.cvs.model.DaysEntity;
import com.cvs.model.EventsEntity;
import com.cvs.model.MandatoryEntity;
import com.cvs.model.UserEntity;
import com.cvs.security.SecurityUtilities;
import com.cvs.services.OfyService;

public class ChmDAOImpl implements ChmDAO {

	private final Objectify objectify = OfyService.ofy();
	
	public void createUserRecord(UserEntity user){
	
		objectify.put(user);
	}
	
	public void createMandatoryRecord(MandatoryEntity mandatory){
		
		objectify.put(mandatory);
	}
	 @Override
	    public List<UserEntity> getUsers() {
	        List<UserEntity> users = new ArrayList<UserEntity>();

	        Query<UserEntity> query = objectify.query(UserEntity.class).order("date");

	        for (UserEntity user : query) {
	            users.add(user);
	        }

	        return users;
	    }
	 
	 	 
	 @Override
	 public List<DaysEntity> getHols(){
	 List<DaysEntity> days = new ArrayList<DaysEntity>();
	 
	 Query<DaysEntity> query = objectify.query(DaysEntity.class).order("date");
	
	 for(DaysEntity day:query){
		 days.add(day);
	 }
	 return days;
	 }
	 
	 @Override
	 public List<DaysEntity> getUnapprovedHols(){
		 List<DaysEntity> days = new ArrayList<DaysEntity>();
		 
		 Query<DaysEntity> query = objectify.query(DaysEntity.class).filter("Accepted", "false").order("date");
		
		 for(DaysEntity day:query){
			 days.add(day);
		 }
		 return days;
		 }
	 
	 @Override
	 public List<DaysEntity> getApprovedHols(){
		 List<DaysEntity> days = new ArrayList<DaysEntity>();
		 
		 SecurityUtilities sec = new SecurityUtilities();
			String user = sec.getLoggedInUserName();
			
			
		 Query<DaysEntity> query =  objectify.query(DaysEntity.class)
				 					.filter("Accepted", "true")
				 					.filter("username", user)
				 					.order("date");
		
		 for(DaysEntity day:query){
			 days.add(day);
		 }
		 return days;
		 }
	 
	 @Override
	public UserEntity searchDatabaseForUsername(String username) {

        // Retrieve all users from the database
        List<UserEntity> users = getUsers();

        // Search user based on the parameters
        for (UserEntity dbUser : users) {
            if (dbUser.getUserName().equals(username) == true) {
                // return matching user
                return dbUser;
            }
        }
        throw new RuntimeException("User does not exist!");
    
	}
	 
	 @Override
	 public DaysEntity searchDatabaseForHolId(Long id) {

	        // Retrieve all ids from the database
	        List<DaysEntity> ids = getUnapprovedHols();

	        // Search id based on the parameters
	        for (DaysEntity dbId : ids) {
	            if (dbId.getId().equals(id) == true) {
	                // return matching id
	                return dbId;
	            }
	        }
	        throw new RuntimeException("Id does not exist!");
	    
		}
	 
	@Override
	public void storeUser(UserEntity user) {
		objectify.put(user);
		
	}
	@Override
	public void createHolidayRecord(DaysEntity day) {
		objectify.put(day);
		
	}
	
	@Override
    public void updateAccepted(Long id, String accepted){
    	
    	if(null != id && null != accepted){
	    	DaysEntity identity = searchDatabaseForHolId(id);
	    	System.out.println("Hi you got to this method Accepted!!");
	    	identity.setAccepted(accepted);
	    	objectify.put(identity);
    	}
    }
	@Override
	public void updateAllocation(String username, int allocation) {
		if(null != username ){
			UserEntity identity = searchDatabaseForUsername(username);
	       	System.out.println("Hi you got to this method Allocated!!");
	    	identity.setAllocation(allocation);
	    	objectify.put(identity);
    	}
		
	}
	@Override
	public void updateGroup(String username, String group) {
		if(null != username ){
			UserEntity identity = searchDatabaseForUsername(username);
	       	System.out.println("Hi you got to this method Group!!");
	    	identity.setGroupName(group);
	    	objectify.put(identity);
    	}
		
	}

	@Override
	public List<MandatoryEntity> getMandatoryDays() {
		List<MandatoryEntity> mandatory = new ArrayList<MandatoryEntity>();
		 
		 Query<MandatoryEntity> query = objectify.query(MandatoryEntity.class);
		System.out.println("in here");
		 for(MandatoryEntity Mandatory:query){
			 mandatory.add(Mandatory);
			 System.out.println("Hit Mando Meth");
		 }
		 return mandatory;
		 
	}

	@Override
	public void createEventRecord(EventsEntity event) {
		objectify.put(event);
		
	}

	@Override
	public List<EventsEntity> getEvents() {
		List<EventsEntity> event = new ArrayList<EventsEntity>();
		
		Query<EventsEntity> query = objectify.query(EventsEntity.class);
		for(EventsEntity Event:query){
			event.add(Event);
		}
		return event;
	}
	
	 

}
